<template>
  <div>
    <a-card :bordered="false">
      <f-search-form @searchHandler="searchHandler">
        <f-search-form-item label="项目代码" name="projectCode" slot="advanced"/>
        <f-search-form-item label="项目名称" name="projectName"/>
        <f-search-form-item label="专业包代码" name="majorCode" slot="advanced"/>
        <f-search-form-item label="专业包名称" name="majorName"/>
        <f-search-form-item label="工程包代码" name="packageNo" slot="advanced"/>
        <f-search-form-item label="工程包名称" name="packageName"/>
      </f-search-form>
      <f-table ref="table" rowKey="materialPackage100Id"
               :showSetting="true"
               :tableColumns="columns"
               tableName="bmp200Index"
               :exportBtn="false"
               :multiple="false"
               :operator="operator" :params="queryParam" :api="api.bmp200" handler="materialPackagePageListHandler">
        <span slot="projectCode" slot-scope="text, record">
          <a-tag color="#108ee9" @click="$refs.bmpBaseDrawer.showDrawer(record)">{{text}}</a-tag>
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="$refs.detailsDrawer.showDrawer(record)">查看详情</a>
            <!--            <a-divider type="vertical"/>-->
            <!--            <a @click="$refs.drawer.showDrawer(record)">编辑供应商</a>-->
          </template>
          <!--          <a-dropdown>-->
          <!--            <a class="ant-dropdown-link">更多 <a-icon type="down"/></a>-->
          <!--            <a-menu slot="overlay">-->
          <!--              <a-menu-item v-if="$auth('table.delete')">-->
          <!--                <a href="javascript:;" @click="$refs.table.del(record)">删除</a>-->
          <!--              </a-menu-item>-->
          <!--            </a-menu>-->
          <!--          </a-dropdown>-->
        </span>
      </f-table>
      <bmp-drawer ref="drawer"></bmp-drawer>
      <bmp-details-drawer ref="detailsDrawer"></bmp-details-drawer>
      <bmp-base-drawer ref="bmpBaseDrawer"></bmp-base-drawer>
    </a-card>
    <div v-if="visible">
      <a-modal
        title="SRM系统价格同步"
        :visible="visible"
        :confirm-loading="confirmLoading"
        @ok="handleOk"
        @cancel="handleCancel"
      >
        <span>项目名称：</span>
        <a-select style="width: 400px" showSearch optionFilterProp="children" @change="valueChange">
          <a-select-option v-for="item in projectList" :key="item.projectId" :value="item.projectId">
            {{ item.projectName }}
          </a-select-option>
        </a-select>
      </a-modal>
    </div>
  </div>
</template>
<script>
  import bmpDrawer from './bmp_drawer.vue'
  import bmpDetailsDrawer from './bmp_details_drawer.vue'
  import bmpBaseDrawer from './bmp_base_drawer.vue'
  import api from './js/api'
  import apiBmp from '../bmp/js/api'

  export default {
    name: 'TableList',
    components: {
      bmpDrawer, bmpDetailsDrawer, bmpBaseDrawer
    },
    data() {
      const me = this
      //操作按钮
      const operator = []
      operator.push({
        name: 'SRM系统价格同步',
        icon: '',
        that: this,
        handler: function(table, that) {
          that.visible = true
        }
      })
      // operator.push({
      //   name: '添加',
      //   icon: 'plus',
      //   that: this,
      //   handler: function(table, that) {
      //       that.$refs.drawer.showDrawer()
      //   }
      // })
      // operator.push({
      //   name: '删除',
      //   icon: 'delete',
      //   batch: true,
      //   handler: function(table) {
      //       table.remove()
      //   }
      // })
      return {
        api: Object.assign(api, this.api),
        apiBmp: Object.assign(apiBmp, this.apiBmp),
        //列表初始化查询参数
        queryParam: { isGroup: true },
        visible: false,
        confirmLoading: false,
        projectId: '',
        projectList: [],
        // 表头
        columns: [
          {
            title: '序号',
            width: '60',
            align: 'center',
            dataIndex: '#',
            customRender: (text, record, index) => index + 1
          },
          {
            title: '项目代码',
            align: 'center',
            dataIndex: 'projectCode',
            scopedSlots: { customRender: 'projectCode' }
          },
          {
            title: '项目名称',
            align: 'center',
            dataIndex: 'projectName'
          },
          {
            title: '工程包名称',
            align: 'center',
            dataIndex: 'packageName'
          },
          {
            title: '工程包代码',
            align: 'center',
            dataIndex: 'packageNo'
          },
          {
            title: '专业名称',
            align: 'center',
            dataIndex: 'majorName'
          },
          {
            title: '专业代码',
            align: 'center',
            dataIndex: 'majorCode'
          },
          {
            title: '最近一次版本提交时间',
            align: 'center',
            dataIndex: 'updateTime'
          },
          {
            title: '操作',
            align: 'center',
            dataIndex: 'action',
            scopedSlots: { customRender: 'action' }
          }
        ],
        operator: operator
      }
    },
    created() {
      this.getProjectList()
    },
    methods: {
      valueChange(val) {
        this.projectId = val
      },
      handleOk(e) {
        this.confirmLoading = true
        setTimeout(() => {
          this.syncSrmPrice()
          this.confirmLoading = false
          this.visible = false
        }, 2000)
      },
      handleCancel(e) {
        this.visible = false
      },
      syncSrmPrice() {
        this.http.post(this, {
          url: this.api.bmp200.syncSrmPrice,
          data: {
            projectId: this.projectId
          },
          success(me, data) {
            if (0 != data.length) {
              this.$message.warn(data)
            }
          }
        })
      },
      getProjectList() {
        this.http.get(this, {
          url: this.apiBmp.bmp.projectList,
          data: {},
          success(me, data) {
            me.projectList = data
          }
        })
      }
    }
  }
</script>
